package FriendTable;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class FriendDAO {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/friend_db";
    private static final String USER = "root";
    private static final String PASS = "12345678";

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(DB_URL, USER, PASS);
    }

    public static void closeConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void addFriend(Friend friend) {
        String sql = "INSERT INTO friends (name, gender, age, favorability, evaluation) VALUES (?, ?, ?, ?, ?)";

        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, friend.getName());
            pstmt.setString(2, Character.toString(friend.getGender()));
            pstmt.setInt(3, friend.getAge());
            pstmt.setInt(4, friend.getFavorability());
            pstmt.setString(5, friend.getEvaluation());

            pstmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static Friend findFriendById(int id) {
        String sql = "SELECT * FROM friends WHERE id = ?";
        Friend friend = null;

        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {
                friend = new Friend(rs.getInt("id"), rs.getString("name"), rs.getString("gender").charAt(0),
                        rs.getInt("age"), rs.getInt("favorability"), rs.getString("evaluation"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return friend;
    }

    public static void updateFriend(Friend friend) {
        String sql = "UPDATE friends SET name = ?, gender = ?, age = ?, favorability = ?, evaluation = ? WHERE id = ?";

        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, friend.getName());
            pstmt.setString(2, Character.toString(friend.getGender()));
            pstmt.setInt(3, friend.getAge());
            pstmt.setInt(4, friend.getFavorability());
            pstmt.setString(5, friend.getEvaluation());
            pstmt.setInt(6, friend.getId());

            pstmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void deleteFriend(int id) {
        String sql = "DELETE FROM friends WHERE id = ?";

        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, id);

            pstmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}